package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.service.AdminService;
import cn.zucc.edu.handyzucc.utils.jwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    private Map<String, Object> login(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        String token = null;
        String username = map.get("username").toString();
        String password = map.get("password").toString();

        if (adminService.login(username,password)) {
            token = jwt.sign(username,password);
            modelMap.put("code", "0");
            modelMap.put("info", "登录成功");
            modelMap.put("token", token);
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "登录失败，请检查信息");
        }
        return modelMap;
    }
}
